package com.bawei.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bawei.mapper.RepertoryMapper;
import com.bawei.model.Repertory;
import com.bawei.model.RepertoryExample;
import com.bawei.model.RepertoryExample.Criteria;
import com.bawei.service.RepertoryService;
@Service
public class RepertoryServiceImpl implements RepertoryService {
	

	
	@Autowired
	private RepertoryMapper repertoryMapper;
	
	@Override
	public void addRepertory(Repertory repertory) {
		// TODO Auto-generated method stub
		int a = repertoryMapper.getCount();                                                                              		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		repertory.setSku(sdf.format(date)+"000"+a);
		repertoryMapper.insert(repertory);
	}

	@Override
	public void updateRepertory(Repertory repertory) {
		// TODO Auto-generated method stub
		repertoryMapper.updateByPrimaryKeySelective(repertory);
	}

	@Override
	public void delRepertory(Long id) {
		// TODO Auto-generated method stub
		repertoryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Repertory> getRepertory(Map map) {
		// TODO Auto-generated method stub
		return repertoryMapper.getRepertory(map);
	}

	@Override
	public Repertory getRepertoryById(Long id) {
		// TODO Auto-generated method stub
		return repertoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Repertory> getRepertoryByCondition(Repertory repertory) {
		// TODO Auto-generated method stub
		RepertoryExample example = new RepertoryExample();
		Criteria criteria = example.createCriteria();
		String sku = repertory.getSku();
		String goodsName = repertory.getGoodsName();
		//String gTel = repertory.get
		/*if(BlankUtil.isNotBlank(gName))
			criteria.andGNameLike("%"+gName+"%");
		if(BlankUtil.isNotBlank(gCode))
			criteria.andGCodeEqualTo(gCode);
		if(BlankUtil.isNotBlank(gTel))
			criteria.andGTelEqualTo(gTel);*/
		return repertoryMapper.selectByExample(example);
	}

	@Override
	public void rk(Repertory repertory) {
		// TODO Auto-generated method stub
		Repertory rep = repertoryMapper.selectByPrimaryKey(repertory.getId());
		rep.setRepertoryNum(rep.getRepertoryNum()+repertory.getRkNum());
		rep.setMidwayNum(rep.getMidwayNum()-repertory.getRkNum());
		repertoryMapper.updateByPrimaryKeySelective(rep);
	}
	
	@Override
	public void ck(Repertory repertory) {
		// TODO Auto-generated method stub
		Repertory rep = repertoryMapper.selectByPrimaryKey(repertory.getId());
		rep.setRepertoryNum(rep.getRepertoryNum()-repertory.getCkNum());
		rep.setLockNum(rep.getLockNum()-repertory.getCkNum());
		repertoryMapper.updateByPrimaryKeySelective(rep);
	}
	

	@Override
	public void relAdd(Repertory repertory) {
		// TODO Auto-generated method stub
		if(repertory.getMidwayNum()==null)
			repertory.setMidwayNum(0);
		if(repertory.getLockNum()==null)
			repertory.setLockNum(0);
		if(repertory.getRepertoryNum()==null)
			repertory.setRepertoryNum(0);
		if(repertory.getRepertoryType()==null)
			repertory.setRepertoryType((byte)0);
		int a = repertoryMapper.getCount()+1;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		repertory.setSku(sdf.format(new Date())+String.format("%04d", a));
		repertoryMapper.insert(repertory);
	}

	@Override
	public void updaterepertoryType(Long id) {
		// TODO Auto-generated method stub
		repertoryMapper.updaterepertoryType(id);
	}

	@Override
	public void updaterepertoryTypeNo(Long id) {
		// TODO Auto-generated method stub
		repertoryMapper.updaterepertoryTypeNo(id);
	}

	
	
	
}
